using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using System.Collections;


namespace OgilvyOne.SqlServerDAL
{
    public class Func : IFunc
    {
        // Fields
        private const string PARM_FUNCTION_CREATE_DATE = "@Function_Create_Date";
        private const string PARM_FUNCTION_DESC = "@Function_Desc";
        private const string PARM_FUNCTION_NAME = "@Function_Name";
        private const string PARM_FUNCTION_UID = "@Function_UID";
        private const string PARM_FUNCTION_UPDATE_DATE = "@Function_Update_Date";
        private const string PARM_ISDEL = "@IsDel";
        private const string SQL_DELETE_FUNCTION = "UPDATE Sys_Function SET IsDel = '1' WHERE Function_UID = @Function_UID";
        private const string SQL_INSERT_FUNCTION = "INSERT INTO Sys_Function VALUES(@Function_Name,@Function_Desc,getDate(),null,'0')";
        private const string SQL_SELECT_FUNCTION = "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function  where Function_UID=@Function_UID and IsDel='0'";
        private const string SQL_SELECT_FUNCTION_LIST = "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0'";
        private const string SQL_SELECT_FUNCTION_SEARCHLIST = "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0' and  Function_UID=@Function_UID and Function_NAME like '%{0}%'";
        private const string SQL_SELECT_FUNCTION_SEARCHNAMELIST = "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0' and Function_NAME like '%{0}%'";
        private const string SQL_UPDATE_FUNCTION = "UPDATE Sys_Function SET Function_Name = @Function_Name, Function_Desc = @Function_Desc, Function_Update_Date = getDate() WHERE Function_UID = @Function_UID";

        // Methods
        public void Delete(int func_id)
        {
            SqlParameter[] functionDeleteParameters = GetFunctionDeleteParameters();
            functionDeleteParameters[0].Value = func_id;
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Function SET IsDel = '1' WHERE Function_UID = @Function_UID", functionDeleteParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public IList getFuncList()
        {
            IList list = new ArrayList();
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0'", null))
            {
                while (reader.Read())
                {
                    FuncInfo info = new FuncInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getFuncList(int funcid)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4, "function_uid") };
            cmdParms[0].Value = funcid;
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, "SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function  where Function_UID=@Function_UID and IsDel='0'", cmdParms))
            {
                while (reader.Read())
                {
                    FuncInfo info = new FuncInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getFuncList(string fName)
        {
            IList list = new ArrayList();
            string cmdText = string.Format("SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0' and Function_NAME like '%{0}%'", fName);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, null))
            {
                while (reader.Read())
                {
                    FuncInfo info = new FuncInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        public IList getFuncList(int fUID, string fName)
        {
            IList list = new ArrayList();
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4, "Function_UID") };
            cmdParms[0].Value = fUID;
            string cmdText = string.Format("SELECT Function_UID,Function_Name,Function_Desc,Function_Create_Date,Function_Update_Date FROM Sys_Function where IsDel='0' and  Function_UID=@Function_UID and Function_NAME like '%{0}%'", fName);
            using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING_NON_DTC, CommandType.Text, cmdText, cmdParms))
            {
                while (reader.Read())
                {
                    FuncInfo info = new FuncInfo(reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
                    list.Add(info);
                }
            }
            return list;
        }

        private static SqlParameter[] GetFunctionDeleteParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Function SET IsDel = '1' WHERE Function_UID = @Function_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4) };
                SQLHelper.CacheParameters("UPDATE Sys_Function SET IsDel = '1' WHERE Function_UID = @Function_UID", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetFunctionInsertParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("INSERT INTO Sys_Function VALUES(@Function_Name,@Function_Desc,getDate(),null,'0')");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Function_Name", SqlDbType.VarChar, 0x40), new SqlParameter("@Function_Desc", SqlDbType.VarChar, 0x200) };
                SQLHelper.CacheParameters("INSERT INTO Sys_Function VALUES(@Function_Name,@Function_Desc,getDate(),null,'0')", cachedParameters);
            }
            return cachedParameters;
        }

        private static SqlParameter[] GetFunctionModifyParameters()
        {
            SqlParameter[] cachedParameters = SQLHelper.GetCachedParameters("UPDATE Sys_Function SET Function_Name = @Function_Name, Function_Desc = @Function_Desc, Function_Update_Date = getDate() WHERE Function_UID = @Function_UID");
            if (cachedParameters == null)
            {
                cachedParameters = new SqlParameter[] { new SqlParameter("@Function_UID", SqlDbType.Int, 4), new SqlParameter("@Function_Name", SqlDbType.VarChar, 0x40), new SqlParameter("@Function_Desc", SqlDbType.VarChar, 0x200) };
                SQLHelper.CacheParameters("UPDATE Sys_Function SET Function_Name = @Function_Name, Function_Desc = @Function_Desc, Function_Update_Date = getDate() WHERE Function_UID = @Function_UID", cachedParameters);
            }
            return cachedParameters;
        }

        public void Insert(FuncInfo acc)
        {
            SqlParameter[] functionInsertParameters = GetFunctionInsertParameters();
            this.SetFunctionInsertParameters(functionInsertParameters, acc);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "INSERT INTO Sys_Function VALUES(@Function_Name,@Function_Desc,getDate(),null,'0')", functionInsertParameters);
                        transaction.Commit();
                    }
                    catch (SqlException exception)
                    {
                        transaction.Rollback();
                        Console.WriteLine(exception.Message);
                    }
                }
            }
        }

        private void SetFunctionInsertParameters(SqlParameter[] parms, FuncInfo acc)
        {
            parms[0].Value = acc.Function_Name;
            parms[1].Value = acc.Function_Desc;
        }

        private void SetFunctionModifyParameters(SqlParameter[] parms, FuncInfo acc)
        {
            parms[0].Value = acc.Function_UID;
            parms[1].Value = acc.Function_Name;
            parms[2].Value = acc.Function_Desc;
        }

        public void Update(FuncInfo myItem)
        {
            SqlParameter[] functionModifyParameters = GetFunctionModifyParameters();
            this.SetFunctionModifyParameters(functionModifyParameters, myItem);
            using (SqlConnection connection = new SqlConnection(SQLHelper.CONN_STRING_NON_DTC))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                {
                    try
                    {
                        SQLHelper.ExecuteNonQuery(transaction, CommandType.Text, "UPDATE Sys_Function SET Function_Name = @Function_Name, Function_Desc = @Function_Desc, Function_Update_Date = getDate() WHERE Function_UID = @Function_UID", functionModifyParameters);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
